<?php

namespace App\Models;

use App\Models\AdminPermission;
use App\Models\AdminRole;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Validator;

class AdminUser extends Model
{
    use DataTables;
    //
    protected $table = 'admin_users';

    public $fillable = ['name','nickname','avatar','email','password','introduction'];

    /**
     *  数据装载保存
     */
    public function loadSave($data , $update_id = 0){
        //数据验证
        $validator = Validator::make($data, $this->rule(), $this->message());
        if($validator->fails()){
            return [
                'code' => 0,
                'status' => 'error',
                'msg' => $validator->errors()->first()
            ];
        }
        if($update_id){
            $obj = AdminUser::find($update_id);
            $obj->name = $data['name'];
            $obj->nickname = $data['nickname'];
            $obj->avatar = $data['avatar'];
            $obj->email = $data['email'];
            $obj->password = md5(md5($data['password']));
            $obj->introduction = $data['introduction'];
            $re = $obj->save();

            //绑定角色
            $role_ids = $data['admin_role'];
            if(!empty($role_ids)){
                $obj->authorizeRoles($role_ids);
            }
        }else{
            $obj = new AdminUser();
            $obj->name = $data['name'];
            $obj->nickname = $data['nickname'];
            $obj->avatar = $data['avatar'];
            $obj->email = $data['email'];
            $obj->password = md5(md5($data['password']));
            $obj->introduction = $data['introduction'];
            $re = $obj->save();

            //绑定角色
            $role_ids = $data['admin_role'];
            if(!empty($role_ids)){
                $obj->authorizeRoles($role_ids);
            }
        }
        if($re){
            return [
                'code' => 1,
                'status' => 'success',
                'msg' => '成功',
            ];
        }else{
            return [
                'code' => 0,
                'status' => 'error',
                'msg' => '失败',
            ];
        }
    }

    /**
     *  验证规则
     */
    protected function rule()
    {
        return [
            'name' => 'required|min:3',
        ];
    }

    /**
     *  验证消息
     */
    protected function message()
    {
        return [
            'name.min' => '名字不能小于3个字符'
        ];
    }

    //用户角色
    public function roles()
    {
        return $this->belongsToMany(AdminRole::class,'admin_role_user','user_id','role_id');
    }

    /**
     * 授权角色
     * @param array $role_ids
     */
    public function authorizeRoles(array $role_ids)
    {
        //清空角色
        $this->roles()->detach();

        if(!empty($role_ids)){
            //授权角色
            $this->roles()->sync($role_ids);
        }
    }

    /**
     * 判断用户是否具有某个角色
     * @param $role
     * @return bool
     */
    public function hasRole($role)
    {
        if (is_numeric($role)) {
            return $this->roles->contains('id', $role);
        }
        if (is_string($role)) {
            return $this->roles->contains('name', $role);
        }

        return !!$role->intersect($this->roles)->count();
    }

    /**
     * 验证用户是否具有某权限
     * @param $permission
     * @return bool
     */
    public function hasPermission($permission)
    {
        if (is_string($permission)) {
            $permission = AdminPermission::where('rule', $permission)->first();
            if (!$permission) return false;
        }
        return $this->hasRole($permission->roles);
    }

}
